package DAO;

import java.sql.Timestamp;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import POJO.MessageResponse;

@Repository
public class MessageResponseDao {
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	/**
	 * 存件短信发送时，记录发送编号
	 * @param messageResponse
	 * @return
	 */
	public int InsertMessageResponse(MessageResponse messageResponse){
			String InsertMessageResponseSql="INSERT INTO `package_sms_status`" +
					" (`package_id`, `message_id`, `message_status`,`message_content`) " +
					"	VALUES (?,?,?,?); ";
			Object[] params=new Object[]{messageResponse.getPackageId(),
										messageResponse.getMessageId(),
										messageResponse.getStatus(),
										messageResponse.getContent()};
			return jdbcTemplate.update(InsertMessageResponseSql,params);
		
	}
	
	/**
	 * 用于九天短信平台上行更新信息
	 * @param messageResponse
	 * @return
	 */
	public int updateMessageResposne(MessageResponse messageResponse){
		try {
			String updateMessageResposneSql="UPDATE `package_sms_status` SET `message_status`=? WHERE `message_id`=?;";
			Object[] params=new Object[]{messageResponse.getStatus(),messageResponse.getMessageId()};
			return jdbcTemplate.update(updateMessageResposneSql,params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	
	/**
	 * 补发短信时，需要更新消息反馈表里的消息id
	 * @param parcelId
	 * @param messageId
	 * @return
	 */
	public int updateMessageIdByParcelId(int parcelId,long messageId){
		try {
			Date date=new Date();
			Timestamp ts=new Timestamp(date.getTime()); 
			String updateMessageIdByParcelIdSql="UPDATE `package_sms_status` SET `message_id`=?,`message_time`=? WHERE `package_id`=?;";
			Object[] params=new Object[]{messageId,ts,parcelId};
			return jdbcTemplate.update(updateMessageIdByParcelIdSql,params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}
	
	/**
	 * 用于vcomcn短信平台上行更新信息
	 * @param searchId
	 * @return
	 */
	public int updateVcomcnMessageResposne(MessageResponse messageResponse){
		try {
			String updateMessageResposneSql="UPDATE `package_sms_status` SET `message_status`=? WHERE `message_id`=?;";
			Object[] params=new Object[]{messageResponse.getStatus(),messageResponse.getMessageId()};
			return jdbcTemplate.update(updateMessageResposneSql,params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
}
